package cn.wolfcode.trip.web.controller;

import cn.wolfcode.trip.base.domain.StrategyContent;
import cn.wolfcode.trip.base.domain.StrategyDetail;
import cn.wolfcode.trip.base.query.StrategyDetailQueryObject;
import cn.wolfcode.trip.base.service.IStrategyDetailService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.util.QiniuCloudUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/strategyDetail")
public class StrategyDetailController {
    @Autowired
    private IStrategyDetailService service;
    @Autowired
    private IStrategyService strategyService;


    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") StrategyDetailQueryObject qo) {
        qo.setOrderBy("sd.releaseTime desc");
        PageInfo<StrategyDetail> pageInfo = service.selectList(qo);
        model.addAttribute("result", pageInfo);
        model.addAttribute("strategies", strategyService.selectEnable());

        return "strategyDetail/list";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(StrategyDetail strategyDetail, MultipartFile img) {
        if (img != null && img.getContentType().startsWith("image") && img.getSize() > 0) {
            String url = QiniuCloudUtil.uploadQiniu(img);
            strategyDetail.setCoverUrl(url);
        }
        service.saveOrUpdate(strategyDetail);
        return "redirect:/strategyDetail/list.do";

    }
    @RequestMapping("/getContentById")
    @ResponseBody
    public Object getContent(Long id){
        StrategyContent content = service.getContentById(id);
        return content;
    }

}
